package com.gofaraway.common.exception.v2;

import com.gofaraway.common.util.PlaceholderUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;

/**
 * @author ChenPeng
 * @Date 2024/7/24 16:22
 */
public class BaseErrorInfoImpl implements BaseErrorInfo {

    private static final long serialVersionUID = 7913532083637174202L;

    /**
     * 错误码
     */
    @Getter
    private final String errorCode;

    /**
     * 错误信息模板
     */
    @Getter
    private final String errorMessageTemplate;

    /**
     * 错误信息占位符
     */
    @Setter
    @Getter
    private Map<String, String> errorMessagePlaceholders;

    public BaseErrorInfoImpl(String errorCode, String errorMessageTemplate) {
        this.errorCode = errorCode;
        this.errorMessageTemplate = errorMessageTemplate;
    }

    public BaseErrorInfoImpl(String errorCode, String errorMessageTemplate, Map<String, String> errorMessagePlaceholders) {
        this.errorCode = errorCode;
        this.errorMessageTemplate = errorMessageTemplate;
        this.errorMessagePlaceholders = errorMessagePlaceholders;
    }

    public BaseErrorInfoImpl(BaseErrorInfo baseErrorInfo) {
        this(baseErrorInfo.getErrorCode(), baseErrorInfo.getErrorMessageTemplate(), baseErrorInfo.getErrorMessagePlaceholders());
    }

    @Override
    public String getErrorMessage() {
        return PlaceholderUtil.parse(errorMessageTemplate, errorMessagePlaceholders);
    }

    @Override
    public String toString() {
        return getErrorCode() + "[" + getErrorMessage() + "]";
    }
}
